import { redirect } from 'react-router-dom'

export async function contentsAction ({ request, params }) {
  const formData = await request.formData()
  const cid = params.cid || 0

  switch (request.method) {
    case 'POST': {
      const id = await window.api.sql(
        `insert into contents (title, content, category_id, created_at) values('未命名片段', '', ${cid}, datetime())`,
        'insert',
      )
      return redirect(`/manager/category/contents/${cid}/content/${id}`)
    }
    case 'DELETE': {
      const id = formData.get('id')

      await window.api.sql(
        'delete from contents where id=@id',
        'del',
        {
          id,
        },
      )
      return {}
    }
  }

  return {} 
}